home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14355 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Nee help with a string and temp string
  5. Date: Sat, 13 Apr 96 16:05:39 GMT
  6. Organization: none
  7. Message-ID: <829411539snz@genesis.demon.co.uk>
  8. References: <316C72CC.763A@cloudnet.com> <316DB246.74FB@willows.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <316DB246.74FB@willows.com>
  15.            tarang@willows.com "Tarang Deshpande" writes:
  16.  
  17. >Randall J. Pfeifer wrote:
  18. >> 
  19. >> I am writing a program that reads data, validates it, and then writes it to a
  20. > new file.
  21. >> ...
  22. >> 
  23. >> My problem involves lines 2 through X.
  24. >>   Reading the data is fine. However, when I evaluate the components and write
  25. > them to the
  26. >>   buffer the previous lines data still resides in the string.
  27.  
  28. >> void examineBcard()
  29. >> { int result;
  30. >> 
  31. >>   strncpy(Bcard,"B",1);
  32. >>   strncat(Bcard,&line[1],1);
  33. >>   strncat(Bcard, Space,3);
  34. >> 
  35. >>   padright(Bcard,5,7,3,3);
  36. >>   padright(Bcard,8,12,5,5);
  37. >>   padright(Bcard,13,18,6,6);
  38. >>   strncat(Bcard," ",1);...
  39. >
  40. >Note that in C the end of a string is marked by a null character or
  41. >char(0).
  42.  
  43. Hold that thought.
  44.  
  45.   Your call to strncpy is equivilant to the following:
  46.  
  47. >Bcard [ 0 ] = 'B';
  48. >Bcard [ 1 ] = '\0';
  49.  
  50. Wrong. strncpy() always writes exactly the number of characters you specify
  51. to the target buffer. It does not guarantee that the result is a string i.e.
  52. null character terminated and is an oddity in the standard string library.
  53. In this case it simply corresponds to:
  54.  
  55.     Bcard[0] = 'B';
  56.  
  57. So the subsequent call to strncat which depends on its first argument
  58. pointing to a null character terminated string isn't going to work properly.
  59. You could fix this by changing strncpy() to simply strcpy() with the
  60. first 2 arguments or you could simplify that group of three lines to:
  61.  
  62.     sprintf(Bcard, "B%c   ", line[1]);
  63.  
  64. or character-wise:
  65.  
  66.     Bcard[0] = 'B';
  67.     Bcard[1] = line[1];
  68.     Bcard[2] = ' ';
  69.     Bcard[3] = ' ';
  70.     Bcard[4] = ' ';
  71.     Bcard[5] = '\0';
  72.  
  73. sprintf is far more flexible though, it can probably replace the calls to
  74. padright as well and the resulting code will be much clearer. I've no idea
  75. what the arguments to padright() specify (I can probably guess the first!)
  76.  
  77. >The call in no way affects Bcard [ x ] where x >= 2.  I suspect that
  78. >the problem is in you padright function which does not put a trailing
  79. >null character at the end. This is one solution.
  80.  
  81. It doesn't fix the immediate problem.
  82.  
  83. -- 
  84. -----------------------------------------
  85. Lawrence Kirby | fred@genesis.demon.co.uk
  86. Wilts, England | 70734.126@compuserve.com
  87. -----------------------------------------
  88.